package com.myapi.client;

import java.util.ArrayList;
import java.util.Date;

import org.apache.tools.ant.taskdefs.Sleep;


import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.ChangeEvent;
import com.extjs.gxt.ui.client.data.ChangeListener;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.DragEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.SliderEvent;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.ProgressBar;
import com.extjs.gxt.ui.client.widget.Slider;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.custom.Portal;
import com.extjs.gxt.ui.client.widget.custom.Portlet;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.RootPanel;
import com.myapi.client.DAO.LoginDAO;
import com.myapi.client.DAO.TableDAO;
import com.myapi.server.PokerServiceImpl;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MyAPI implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";

	/**
	 * Create a remote service xy to talk to the server-side Greeting service.
	 */
	private final PokerServiceAsync pokerService = GWT
	.create(PokerService.class);
	private MapWidget map;

	private VerticalPanel mainPanel = new VerticalPanel();
	private FlexTable stocksFlexTable = new FlexTable();
	private HorizontalPanel addPanel = new HorizontalPanel();
	private TextBox newSymbolTextBox = new TextBox();
	private Button addStockButton = new Button("Add");
	private Label lastUpdatedLabel = new Label();
	private ArrayList<String> stocks = new ArrayList<String>();
	private static final int REFRESH_INTERVAL = 500; // ms

	private TextBox textbox=new TextBox();
	TextBox txtb_TxtChat;
	private TableDAO tableDAO=new TableDAO();
	private String myId="";
	private Image img_Player1Card2;
	private Image img_MyCard1;
	private Image img_MyCard2;
	private Image img_Player3Card2;
	private Image img_Player3Card1;
	private Image img_Player2Card2;
	private Image img_Player2Card1;
	private Image img_Player1Card1;
	private Image img_Player4Card2;
	private Image img_Player4Card1;
	private Image img_Player2;
	private Label lbl_namePlayer2;
	private Label lbl_chipsPlayer2;
	private Image img_Player3;
	private Label lbl_namePlayer3;
	private Label lbl_chipsPlayer3;
	private Image img_Player4;
	private Label lbl_namePlayer4;
	private Label lbl_chipsPlayer4;
	private Image img_Player1;
	private Label lbl_namePlayer1;
	private Label lbl_chipsPlayer1;
	private Label lbl_chipsTable;
	private Image img_CardFlop1;
	private Image img_CardFlop2;
	private Image img_CardFlop3;
	private Image img_CardTurn;
	private Image img_CardRiver;
	private TextArea txt_forum;
	private HorizontalPanel panel_Cards;
	private RootPanel rootPanel;
	private ProgressBar progressBar;
	private Button btn_Fold;
	private Button btn_CheckCall;
	private Button btn_Raise;
	private Button btn_LeaveTable;
	Boolean activeTimer=false;
	double CurrentBetAmout=0.0;

	boolean preflop=false;
	boolean flop=false;
	boolean turn=false;
	boolean river=false;


	int TIMEMAXPLAY=15;
	Timer timerPlayer;
	int timeCurrentPlayer;



	private LoginDAO loginDAO = null;
	private VerticalPanel loginPanel = new VerticalPanel();
	private Label loginLabel = new Label("Please sign in to your Google Account to access the Poker application.");
	private Anchor signInLink = new Anchor("Sign In");
	private Anchor signOutLink = new Anchor("Sign Out");

	private Slider slide;

	private TextBox txt_Raise;
	StatisticUI statUI;




	public void onModuleLoad() {
		loginDAO=new LoginDAO();
		myId=""+(Math.random()*100);
		loginDAO.setEmailAddress(myId);
		loginDAO.setNickname(myId);

		rootPanel = RootPanel.get();
		statUI=new StatisticUI(loginDAO);
		
		Button stat=new Button("Stat");
		stat.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				getstatUI();

			}

		});
		
		rootPanel.add(stat);
		rootPanel.add(new UI(new TableUI(loginDAO),statUI));

		


	}

	public void getstatUI() {
		statUI.getStatistic();

	}

	private void loadLogin() {
		// Assemble login panel.
		signInLink.setHref(loginDAO.getLoginUrl());
		loginPanel.add(loginLabel);
		loginPanel.add(signInLink);
		RootPanel.get("Login").add(loginPanel);


	}

	public void onModuleLoa() {
		// Check login status using login service.
		LoginServiceAsync loginService = GWT.create(LoginService.class);
		loginService.login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginDAO>() {
			public void onFailure(Throwable error) {
			}

			public void onSuccess(LoginDAO result) {
				loginDAO = result;
				if(loginDAO.isLoggedIn()) {
					myId=loginDAO.getEmailAddress();
					TableUI tableUI=new TableUI(loginDAO);

				} else {
					loadLogin();
				}
			}
		});




	}
	class UI extends LayoutContainer{
		TableUI tableUI;
		StatisticUI statisticUI;
		public UI(TableUI tableUI,StatisticUI statisticUI) {
			this.tableUI=tableUI;
			this.statisticUI=statisticUI;

		}
		protected void onRender(Element parent, int index) {  



			super.onRender(parent, index);  
			setLayout(new BorderLayout());  

			LayoutContainer north = new LayoutContainer();  
			BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 30);  

			ContentPanel west = new ContentPanel();  
			west.setBodyBorder(false);  
			west.setHeading("Information");  
			west.setLayout(new AccordionLayout());  

			ContentPanel nav = new ContentPanel();  
			nav.setHeading("Statistic");  
			nav.setBorders(false);  
			nav.setBodyStyle("fontSize: 12px; padding: 6px"); 
			nav.addListener(Events.Select, new Listener<ComponentEvent>() {  
				public void handleEvent(ComponentEvent be) {  
					Window.alert("Event Tab Was Selected");  
				}  
			}); 
			west.add(nav);  

			nav = new ContentPanel();  
			nav.setHeading("Settings");  
			nav.setBorders(false);  
			nav.setBodyStyle("fontSize: 12px; padding: 6px"); 
			west.add(nav);  

			ContentPanel settings = new ContentPanel();  
			settings.setHeading("Table");  
			settings.setBorders(false);  
			west.add(settings);  



			BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 200, 100, 300);  
			westData.setMargins(new Margins(5, 0, 5, 5));  
			westData.setCollapsible(true);  


			/*	Portlet portlet = new Portlet(); 
			portlet.setLayout(new FitLayout());
			portlet.setHeading("Grid in a Portlet");  
			configPanel(portlet);    
			portlet.setSize("780", "610");  
			portlet.add(statisticUI);
			 */


		/*	Portal portal = new Portal(2);  
			portal.setSize("1000", "1000");
			portal.setBorders(true);  
			portal.setStyleAttribute("backgroundColor", "white");  
			portal.setColumnWidth(0, .5);  
			portal.setColumnWidth(1, .5);   
*/
			Portlet portlet = new Portlet(); 
			portlet.setLayout(new FitLayout());
			portlet.setHeading("Table Poker");  
			configPanel(portlet);    
			portlet.setSize("780", "610");  
			portlet.add(tableUI);

			BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
			centerData.setMargins(new Margins(5));  

			add(north, northData);  
			add(west, westData);  
			add(portlet, centerData);  
		}  




		private void configPanel(final ContentPanel panel) {  
			panel.setCollapsible(true);  
			panel.setAnimCollapse(false);  
			panel.getHeader().addTool(new ToolButton("x-tool-gear"));  
			panel.getHeader().addTool(  
					new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  

						@Override  
						public void componentSelected(IconButtonEvent ce) {  
							panel.removeFromParent();  
						}  

					}));  
		}  

	}
}